package com.lvmama.controller.rhino.searchstatic;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lvmama.client.rhino.SearchStatisticsClient;
import com.lvmama.common.enums.CommonParentType;
import com.lvmama.common.enums.CommonSearchFilter;
import com.lvmama.common.enums.CommonSearchType;
import com.lvmama.common.enums.CommonStringType;
import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.DateUtil;
import com.lvmama.common.utils.JSONConversionUtil;
import com.lvmama.pojo.dto.rhino.searchanalysis.*;
import com.lvmama.pojo.entity.rhino.searchanalysis.FlowSearchStation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import static com.lvmama.common.utils.DateUtil.dateFormat;

/**
 * 搜索分析菜单
 */
@Controller
@RequestMapping("searchStatistic")
@Slf4j
public class SearchStatisticsController {
    @Autowired
    SearchStatisticsClient searchStatisticsClient;

    /**
     * 搜索流量统计页
     * @return
     */
    @RequestMapping(value = "searchFlux",method = RequestMethod.GET)
    public String searchFlux() {
        return "searchStatistic/searchFlux";
    }

    /**
     * 搜索流量统计页获取图表数据
     * @param queryParam
     * @return
     */
    @RequestMapping(value = "searchAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> searchAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController searchAjaxData start ");
        List<FlowSearchStatistics> searchByChanneList = null;
        List<FlowSearchStatistics> searchByDateList = null;
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        try {
            searchByChanneList = searchStatisticsClient.getSearchFlowByChannel(queryParam).getData();
            searchByDateList = searchStatisticsClient.getSearchFlowByDate(queryParam).getData();
        } catch (Exception e) {
            log.error("SearchStatisticsController searchAjaxData error :{}", e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        //折线图数据
        List<LineDataVo> lineData = new ArrayList<>();
        if (searchByChanneList != null) {
            Map<String, Long> mapForLine = new HashMap<String, Long>();
            for (FlowSearchStatistics flowSearchStatistics : searchByDateList) {
                String channelCode = getChannelName(flowSearchStatistics.getChannelCode());
                String date = DateUtil.dateFormat(flowSearchStatistics.getOperDate(), "yyyy/MM/dd");
                String key = channelCode + "_" + date;
                long amount = mapForLine.containsKey(key) ? flowSearchStatistics.getAmount() + mapForLine.get(key) : flowSearchStatistics.getAmount();
                mapForLine.put(key, amount);
            }
            lineData = parseJsonForLine(mapForLine);
        }
        //柱状图数据
        List<BarDataVo> barData = new ArrayList<>();
        if (searchByChanneList != null) {
            Map<String, Long> mapForBar = new HashMap<String, Long>();
            for (FlowSearchStatistics flowSearchStatistics : searchByChanneList) {
                String channelCode = getChannelName(flowSearchStatistics.getChannelCode());
                long amount = mapForBar.containsKey(channelCode) ? mapForBar.get(channelCode) + flowSearchStatistics.getAmount()
                        : flowSearchStatistics.getAmount();
                mapForBar.put(channelCode, amount);
            }
            barData = parseJsonForBar(mapForBar);
        }
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setBarData(barData);
        searchStatisticsResult.setLineData(lineData);
        log.info("SearchStatisticsController searchAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    /**
     * 关键字分析
     * @param model
     * @return
     */
    @RequestMapping(value = "keyword",method = RequestMethod.GET)
    public String keyword(ModelMap model) {
        List<FlowSearchStation> stationList = searchStatisticsClient.getStations().getData();
        model.addAttribute("stationList", stationList);
        return "searchStatistic/keyword";
    }

    /**
     * 关键字分析获取数据
     * @param queryParam
     * @return
     */
    @RequestMapping(value = "keyAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> keyAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController keyAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        List<FlowSearchKeyType> serachListByKey = null;
        List<FlowSearchKeyType> serachListByDate = null;
        try {
            serachListByKey = searchStatisticsClient.getKeyByType(queryParam).getData();
            serachListByDate = searchStatisticsClient.getKeyByDate(queryParam).getData();
        } catch (Exception e) {
            log.info("SearchStatisticsController keyAjaxData error :{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        /* 柱状图数据封装 */
        List<BarDataVo> barData = new ArrayList<BarDataVo>();
        for (FlowSearchKeyType flowSearchKey : serachListByKey) {
            String type = CommonStringType.getKeyNameByValue(flowSearchKey.getKeyType());
            if (StringUtils.isNotEmpty(type)) {
                BarDataVo vo = new BarDataVo();
                vo.setAmount(flowSearchKey.getAmount());
                vo.setCategory(type);
                barData.add(vo);
            }
        }
        /* 折线图数据封装 */
        Map<String, Long> mapForLine = new HashMap<String, Long>();
        for (FlowSearchKeyType flowSearchKey : serachListByDate) {
            String type = CommonStringType.getKeyNameByValue(flowSearchKey.getKeyType());
            String date = DateUtil.dateFormat(flowSearchKey.getOperDate(),"yyyy/MM/dd");
            String key = type + "_" + date;
            long amount = mapForLine.containsKey(key)?flowSearchKey.getAmount()+mapForLine.get(key):flowSearchKey.getAmount();
            mapForLine.put(key, amount);
        }
        List<LineDataVo> lineData = parseJsonForLine(mapForLine);

        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setLineData(lineData);
        searchStatisticsResult.setBarData(barData);
        log.info("SearchStatisticsController keyAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "keyWordAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<FlowSearchKey>> keyWordAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController keyWordAjaxData start");
        try {
            List<SearchConditionVO> conditionList = new ArrayList<SearchConditionVO>();
            String sectionIds = queryParam.getSectionIds();
            if (StringUtils.isNotEmpty(sectionIds)) {
                for (String sectionId : sectionIds.substring(0,sectionIds.length() - 1).split(",")){
                    conditionList.add(getSectionCondition(Integer.parseInt(sectionId)));
                }
            }
            queryParam.setConditionList(conditionList);
            PageInfo<FlowSearchKey> page = searchStatisticsClient.getKeyByAmount(queryParam).getData();
            log.info("SearchStatisticsController keyWordAjaxData end");
            return Result.success(page);
        } catch (Exception e) {
            log.error("SearchStatisticsController keyWordAjaxData error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
    }

    @RequestMapping(value = "typeAStationAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> typeAStationAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController typeAStationAjaxData start");
        List<SearchConditionVO> conditionList = new ArrayList<SearchConditionVO>();
        String sectionIds = queryParam.getSectionIds();
        if (StringUtils.isNotEmpty(sectionIds)) {
            for (String sectionId : sectionIds.substring(0,sectionIds.length() - 1).split(",")){
                conditionList.add(getSectionCondition(Integer.parseInt(sectionId)));
            }
        }
        queryParam.setConditionList(conditionList);

        List<FlowSearchKey> serachForStations = searchStatisticsClient.getDetailByStation(queryParam).getData();
        List<FlowSearchKey> serachForType = searchStatisticsClient.getDetailByType(queryParam).getData();

        Map<String, Integer> keyTypeMap = new HashMap<String, Integer>();
        for (FlowSearchKey flowSearchKey : serachForType) {
            String strKeyTypeMap = flowSearchKey.getKeyTypeMap();
            if (StringUtils.isNotEmpty(strKeyTypeMap)) {
                JSONObject object = JSONConversionUtil.stringToObj(flowSearchKey.getKeyTypeMap(),JSONObject.class);
                for (Entry<String, Object> entry : object.entrySet()) {
                    String key = entry.getKey();
                    int value = Integer.parseInt(entry.getValue().toString());
                    int totalValue = keyTypeMap.containsKey(key)?keyTypeMap.get(key)+value:value;
                    keyTypeMap.put(key, totalValue);
                }
            }
        }
        /* type数据封装 */
        List<BarDataVo> keyDataList = new ArrayList<BarDataVo>();
        for (Entry<String, Integer> entry : keyTypeMap.entrySet()) {
            String type = CommonStringType.getKeyNameByValue(entry.getKey());
            if (StringUtils.isNotEmpty(type)) {
                BarDataVo vo = new BarDataVo();
                vo.setAmount(entry.getValue());
                vo.setCategory(type);
                keyDataList.add(vo);
            }
        }
        Map<String, Integer> stationMap = new HashMap<String, Integer>();
        for (FlowSearchKey flowSearchKey : serachForStations) {
            String provMap = flowSearchKey.getProvMap();
            if (StringUtils.isNotEmpty(provMap)) {
                JSONObject object = JSONConversionUtil.stringToObj(provMap,JSONObject.class);
                for (Entry<String, Object> entry : object.entrySet()) {
                    String key = entry.getKey();
                    int value = Integer.parseInt(entry.getValue().toString());
                    int totalValue = stationMap.containsKey(key)?stationMap.get(key)+value:value;
                    stationMap.put(key, totalValue);
                }
            }
        }
        //降序排列,取前10个 station数据封装处理
        List<BarDataVo> stationList = stationMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(10)
                .map(v->new BarDataVo(v.getKey(),v.getValue())).collect(Collectors.toList());

        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setKeyData(keyDataList);
        searchStatisticsResult.setStationData(stationList);
        log.info("SearchStatisticsController typeAStationAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "keyForCategoryAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> keyForCategoryAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController keyForCategoryAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        List<FlowSearchArrivalKey> searchByCategoryList = searchStatisticsClient.getKeyByCategory(queryParam).getData();
        List<BarDataVo> keyCategoryDataList = new ArrayList<BarDataVo>();
        for (FlowSearchArrivalKey flowSearchKey : searchByCategoryList) {
            String name = CommonParentType.getCategoryShowName(flowSearchKey.getCategoryId());
            if (StringUtils.isNotEmpty(name)) {
                BarDataVo vo = new BarDataVo();
                vo.setAmount(flowSearchKey.getSearchKeyAmount());
                vo.setCategory(name);
                keyCategoryDataList.add(vo);
            }
        }
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setKeyCategoryData(keyCategoryDataList);
        log.info("SearchStatisticsController keyForCategoryAjaxData end");
        return Result.success(searchStatisticsResult);
    }
    @RequestMapping(value = "keyForKeyWordAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> keyForKeyWordAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController keyForKeyWordAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        queryParam.setCategoryId(CommonParentType.getCategoryValueByShowName(queryParam.getCategoryName()));
        List<FlowSearchArrivalKey> searchByWordList = searchStatisticsClient.getKeyByKeyword(queryParam).getData();

        Map<String, Integer> keyMap = new HashMap<String, Integer>();
        for (FlowSearchArrivalKey flowSearchKey : searchByWordList) {
            keyMap.put(flowSearchKey.getSearchKey(), flowSearchKey.getSearchKeyAmount());
        }
        List<BarDataVo> keyWordDataList = new ArrayList<BarDataVo>();
        //降序排列,取前10个
        keyWordDataList = keyMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(10)
              .map(v->new BarDataVo(v.getKey(),v.getValue())).collect(Collectors.toList());
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setKeyWordData(keyWordDataList);
        log.info("SearchStatisticsController keyForKeyWordAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "keyForXgWordAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<ProductInfoVO>> keyForXgWordAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController keyForXgWordAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        queryParam.setCategoryId(CommonParentType.getCategoryValueByShowName(queryParam.getCategoryName()));
        List<FlowSearchArrivalKey> keyXgwordList = searchStatisticsClient.getKeyByXgword(queryParam).getData();
        List<ProductInfoVO> productVoList = new ArrayList<ProductInfoVO>();
        Map<String, Integer> productMap = new HashMap<String, Integer>();
        for (FlowSearchArrivalKey flowSearchArrivalKey : keyXgwordList) {
            String map = flowSearchArrivalKey.getSearchMap();
            JSONObject jsonObject = JSONConversionUtil.stringToObj(map,JSONObject.class);
            for(Entry<String,Object> entry :jsonObject.entrySet()){
                String key = entry.getKey();
                int value = Integer.parseInt(entry.getValue().toString());
                int totalValue = productMap.containsKey(key)?value+productMap.get(key) :value;
                productMap.put(key, totalValue);
            }
        }
        if (queryParam.getFlag() == 1) {
            //升序排列
            productMap = productMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
                    .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
        } else {
            //降序排列
            productMap = productMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                    .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
        }
        for (Entry<String, Integer> entry : productMap.entrySet()) {
            ProductInfoVO vo = new ProductInfoVO();
            String[] keyArr = new String[2];
            keyArr[0] = entry.getKey().split(",")[0];
            if (entry.getKey().split(",").length > 1) {
                keyArr[1] = entry.getKey().split(",")[1];
            }
            vo.setProductId(keyArr[0]);
            vo.setProductName(keyArr[1] == null ? "" : keyArr[1]);
            vo.setProductNum(entry.getValue());
            productVoList.add(vo);
        }
        PageInfo<ProductInfoVO> page = getListByPage(productVoList, queryParam.getPageSize(),queryParam.getPageCurrentNum());
        log.info("SearchStatisticsController keyForXgWordAjaxData end");
        return Result.success(page);
    }

    @RequestMapping(value = "skipAnalysis",method = RequestMethod.GET)
    public String skipAnalysis(ModelMap modelMap) {
        List<FlowSearchStation> stationList = searchStatisticsClient.getStations().getData();
        modelMap.addAttribute("stationList", stationList);
        return "searchStatistic/skipAnalysis";
    }

    @RequestMapping(value = "jumpAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> jumpAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController jumpAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        if ("pv".equals(queryParam.getFlowType())) {
            queryParam.setJumpTableName("flow_search_jump_pv");
        } else {
            queryParam.setJumpTableName("flow_search_jump_uv");
        }
        List<FlowSearchJump> searchByCategoryList = new ArrayList<>();
        List<FlowSearchJump> searchByDatelList = new ArrayList<>();
        if ("切换标签".equals(queryParam.getKeyType())) {
            searchByCategoryList = searchStatisticsClient.getSwitchByCategory(queryParam).getData();
            searchByDatelList = searchStatisticsClient.getSwitchByDate(queryParam).getData();
        } else {
            searchByCategoryList = searchStatisticsClient.getJumpByCategory(queryParam).getData();
            searchByDatelList = searchStatisticsClient.getJumpByDate(queryParam).getData();
        }
        /* 柱状图数据封装 */
        List<BarDataVo> barDataList = searchByCategoryList.stream().filter(v->CommonSearchType.getShownameByValue(v.getCategoryCode())!=null).
                map(v->new BarDataVo(CommonSearchType.getShownameByValue(v.getCategoryCode()),v.getAmount())).collect(Collectors.toList());

        /* 折线图数据封装 */
        Map<String, Long> mapForLine = new HashMap<String, Long>();
        for (FlowSearchJump flowSearchJump : searchByDatelList) {
            String categoryName = CommonSearchType.getShownameByValue(flowSearchJump.getCategoryCode());
            String date = DateUtil.dateFormat(flowSearchJump.getOperDate(),"yyyy/MM/dd");
            String key = categoryName + "_" + date;
            long amount = mapForLine.containsKey(key)?flowSearchJump.getAmount()+mapForLine.get(key):flowSearchJump.getAmount();
            mapForLine.put(key, amount);
        }
        List<LineDataVo> lineDataList = parseJsonForLine(mapForLine);

        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setLineData(lineDataList);
        searchStatisticsResult.setBarData(barDataList);
        log.info("SearchStatisticsController jumpAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "jumpByCategoryAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> jumpByCategoryAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController jumpByCategoryAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        queryParam.setCategoryCode(CommonSearchType.getValueByShowName(queryParam.getCategoryName()));
        if ("pv".equals(queryParam.getFlowType())) {
            queryParam.setJumpTableName("flow_search_jump_pv");
        } else {
            queryParam.setJumpTableName("flow_search_jump_uv");
        }

        List<FlowSearchJump> searchList = new ArrayList<FlowSearchJump>();

        if ("切换标签".equals(queryParam.getKeyType())) {
            searchList = searchStatisticsClient.getSwitchByResult(queryParam).getData();
        } else {
            searchList = searchStatisticsClient.getMoJumpByResult(queryParam).getData();
        }
        int successTotal = 0;
        int failTotal = 0;
        int switchSuccessTotal = 0;
        int switchFailTotal = 0;
        Map<String, Integer> keyAmountMap = new HashMap<>();

        for (FlowSearchJump flowSearchJump : searchList) {
            if ("切换标签".equals(queryParam.getKeyType())) {
                switchSuccessTotal += flowSearchJump.getSwitchJumpSuccess();
                switchFailTotal += flowSearchJump.getJumpFailure();

                if (StringUtils.isNotBlank(flowSearchJump.getSwitchJumpJson())
                        && !"{}".equals(flowSearchJump.getSwitchJumpJson())) {
                    JSONObject jsonObject = JSONConversionUtil.stringToObj(flowSearchJump.getSwitchJumpJson(),JSONObject.class);
                    for(Entry<String,Object> entry :jsonObject.entrySet()){
                        String key = entry.getKey();
                        int value = Integer.parseInt(entry.getValue().toString());
                        if (keyAmountMap.containsKey(key)) {
                            value = keyAmountMap.get(key) + value;
                        }
                        keyAmountMap.put(key, value);
                    }
                }
            } else {
                successTotal += flowSearchJump.getJumpSuccess();
                failTotal += flowSearchJump.getJumpFailure();
            }
        }
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        if ("切换标签".equals(queryParam.getKeyType())) {
            List<ProductInfoVO> list = new ArrayList<ProductInfoVO>();
            keyAmountMap = keyAmountMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                    .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
            for (Entry<String, Integer> entry : keyAmountMap.entrySet()) {
                ProductInfoVO vo = new ProductInfoVO();
                vo.setProductName(entry.getKey());
                vo.setProductNum(entry.getValue());
                list.add(vo);
            }
            PageInfo<ProductInfoVO> page = getListByPage(list, queryParam.getPageSize(),queryParam.getPageCurrentNum());
            searchStatisticsResult.setSwitchSuccessTotal(switchSuccessTotal);
            searchStatisticsResult.setSwitchFailTotal(switchFailTotal);
            searchStatisticsResult.setPage(page);
            List<BarDataVo> dataList = new ArrayList<BarDataVo>();
            BarDataVo vo2 = new BarDataVo();
            vo2.setAmount(switchFailTotal);
            vo2.setCategory("跳转失败");
            dataList.add(vo2);
            BarDataVo vo = new BarDataVo();
            vo.setAmount(switchSuccessTotal);
            vo.setCategory("直接跳转成功");
            dataList.add(vo);
            searchStatisticsResult.setBarData(dataList);
        } else {
            List<BarDataVo> barDataList = new ArrayList<BarDataVo>();
            BarDataVo vo2 = new BarDataVo();
            vo2.setAmount(failTotal);
            vo2.setCategory("跳转失败");
            barDataList.add(vo2);
            BarDataVo vo = new BarDataVo();
            vo.setAmount(successTotal);
            vo.setCategory("切换跳转成功");
            barDataList.add(vo);
            searchStatisticsResult.setBarData(barDataList);
            searchStatisticsResult.setSuccessTotal(successTotal);
            searchStatisticsResult.setFailTotal(failTotal);
        }
        log.info("SearchStatisticsController jumpByCategoryAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    /**
     * @Description: 搜索结果分析
     * @Title: resultAnalysis
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "resultAnalysis",method = RequestMethod.GET)
    public String resultAnalysis(ModelMap modelMap) {
        List<FlowSearchStation> stationList = searchStatisticsClient.getStations().getData();
        modelMap.addAttribute("stationList", stationList);
        return "searchStatistic/resultAnalysis";
    }

    @RequestMapping(value = "resultAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> resultAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController resultAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);

        List<FlowSearchResult> searchByCategoryList = searchStatisticsClient.getResultByCategory(queryParam).getData();
        /* 柱状图数据封装 */
        List<BarDataVo> barDataList = new ArrayList<BarDataVo>();
        for (FlowSearchResult flowSearchResult : searchByCategoryList) {
            String category = CommonSearchType.getShownameByValue(flowSearchResult.getCategoryCode());
            if (StringUtils.isNotEmpty(category)) {
                BarDataVo vo = new BarDataVo();
                vo.setAmount(flowSearchResult.getAmountPV());
                vo.setCategory(category);
                barDataList.add(vo);
            }
        }
        /* 折线图数据封装 */
        List<FlowSearchResult> searchByDatelList = searchStatisticsClient.getResultByDate(queryParam).getData();
        Map<String, Long> mapForLine = new HashMap<String, Long>();
        for (FlowSearchResult flowSearchResult : searchByDatelList) {
            String categoryName = CommonSearchType.getShownameByValue(flowSearchResult.getCategoryCode());
            String date = dateFormat(flowSearchResult.getOperDate(),"yyyy/MM/dd");
            String key = categoryName + "_" + date;
            long amount = mapForLine.containsKey(key)?flowSearchResult.getAmountPV() + mapForLine.get(key):flowSearchResult.getAmountPV();
            mapForLine.put(key, amount);
        }
        List<LineDataVo> lineDataList = parseJsonForLine(mapForLine);

        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setLineData(lineDataList);
        searchStatisticsResult.setBarData(barDataList);
        log.info("SearchStatisticsController resultAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "resultKeyAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<FlowSearchResult>> resultKeyAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController resultKeyAjaxData start");
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        queryParam.setCategoryId(CommonSearchType.getValueByShowName(queryParam.getCategoryName()));

        PageInfo<FlowSearchResult> page = searchStatisticsClient.getResultByKey(queryParam).getData();
        log.info("SearchStatisticsController resultKeyAjaxData end");
        return Result.success(page);
    }

    /**
     * 筛选条件分析
     *
     * @param modelMap
     * @return 页面
     */
    @RequestMapping(value = "screeningAnalysis",method = RequestMethod.GET)
    public String screeningAnalysis(ModelMap modelMap) {
        CommonSearchType[] typeArr = CommonSearchType.getLabType();
        modelMap.addAttribute("typeArr", typeArr);
        return "/searchStatistic/screeningAnalysis";
    }

    @RequestMapping(value = "filterAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> filterAjaxData(SearchStatisticsQuery queryParam, HttpSession httpSession) {
        log.info("SearchStatisticsController filterAjaxData start");

        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);

        String[] dateAxis = DateUtil.getBetweenTime(queryParam.getBeginDate(), queryParam.getEndDate(),"yyyy/MM/dd");

        // json串 按照日期 获取搜索条件对象list 注：<日期,搜索条件list>
        Map<String, List<FilterConditionVO>> dataFilterMap = new HashMap<String, List<FilterConditionVO>>();

        String key = queryParam.getBeginDate().toString()+ queryParam.getEndDate().toString() + queryParam.getPlatformCode() + queryParam.getCategoryId() + "dataFilterMap";
        if (httpSession.getAttribute(key) != null) {
            dataFilterMap = (Map<String, List<FilterConditionVO>>) httpSession.getAttribute(key);
        } else {
            List<FlowSearchFilter> filterList = searchStatisticsClient.getFilterList(queryParam).getData();
            for (FlowSearchFilter flowSearchFilter : filterList) {
                String filterCondition = flowSearchFilter.getFilterCondition().trim();
                List<FilterConditionVO> listVO = getVoList(filterCondition,queryParam.getCategoryId());

                if (dataFilterMap.containsKey(DateUtil.formatDate(flowSearchFilter.getOperDate(), "yyyy/MM/dd"))) {
                    listVO.addAll(dataFilterMap.get(DateUtil.formatDate(flowSearchFilter.getOperDate(), "yyyy/MM/dd")));
                }
                dataFilterMap.put(DateUtil.formatDate(flowSearchFilter.getOperDate(), "yyyy/MM/dd"), listVO);
            }
            httpSession.setAttribute(key, dataFilterMap);
        }

        // <搜索条件,时间段内逐日的搜素量数组>
        Map<String, Long> amountLineMap = new HashMap<String, Long>();
        Map<String, Long> amountBarMap = new HashMap<String, Long>();
        if (dataFilterMap!=null&&!dataFilterMap.isEmpty()) {
            for (int i = 0; i < dateAxis.length; i++) {
                if (dataFilterMap.containsKey(dateAxis[i])) {
                    List<FilterConditionVO> list = dataFilterMap.get(dateAxis[i]);
                    for (FilterConditionVO filterConditionVO : list) {
                        // 按照 搜索条件 统计出当前搜索条件每日的搜索系数
                        int amount = 0;
                        if (filterConditionVO.getChildren() == null || filterConditionVO.getChildren().size() == 0) {
                            amount = filterConditionVO.getAmount();
                        } else {
                            List<SecondConditionVO> voList = filterConditionVO.getChildren();
                            int childAmount = 0;
                            for (SecondConditionVO vo : voList) {
                                if (vo.getSechildren() != null && vo.getSechildren().size() != 0) {
                                    for (ChildConditionVO child : vo.getSechildren()) {
                                        childAmount += child.getAmount();
                                    }
                                } else {
                                    childAmount += vo.getAmount();
                                }
                            }
                            amount = childAmount;
                        }
                        long filterAmount = 0l;
                        long filterBarAmount = 0l;
                        if (StringUtils.isNotEmpty(filterConditionVO
                                .getCondition())) {
                            // 折线图数据封装---搜索条件,时间 相同 map的数量加上当前的数量
                            if (amountLineMap.containsKey(filterConditionVO.getCondition() + "_" + dateAxis[i])) {
                                filterAmount = amountLineMap.get(filterConditionVO.getCondition()+ "_" + dateAxis[i]);
                            }
                            filterAmount += amount;
                            amountLineMap.put(filterConditionVO.getCondition()+ "_" + dateAxis[i], filterAmount);

                            // 柱状图数据封装--- 搜索条件 相同 map的数量加上当前的数量
                            if (amountBarMap.containsKey(filterConditionVO.getCondition())) {
                                filterBarAmount = amountBarMap.get(filterConditionVO.getCondition());
                            }
                            filterBarAmount += amount;
                            amountBarMap.put(filterConditionVO.getCondition(),filterBarAmount);
                        }
                    }
                }
            }
        }
        List<LineDataVo> lineData = parseJsonForLine(amountLineMap);
        List<BarDataVo> barData = parseJsonForBar(amountBarMap);
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setBarData(barData);
        searchStatisticsResult.setLineData(lineData);
        log.info("SearchStatisticsController filterAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "childrenAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> childrenAjaxData(SearchStatisticsQuery queryParam, String type,HttpSession httpSession) {
        log.info("SearchStatisticsController childrenAjaxData start");
        Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
        // 获取平台相关code
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        List<FilterConditionVO> list = new ArrayList<FilterConditionVO>();

        String key = queryParam.getBeginDate().toString()+queryParam.getEndDate().toString() + queryParam.getPlatformCode() +
                queryParam.getCategoryId() + "dataFilterList";
        if (httpSession.getAttribute(key) != null) {
            list = (List<FilterConditionVO>) httpSession.getAttribute(key);
        } else {
            List<FlowSearchFilter> filterList = searchStatisticsClient.getFilterList(queryParam).getData();
            for (FlowSearchFilter flowSearchFilter : filterList) {
                String filterCondition = flowSearchFilter.getFilterCondition().trim();
                list.addAll(getVoList(filterCondition, queryParam.getCategoryId()));
            }
            httpSession.setAttribute(key, list);
        }

        int code = 1;
        List<String> seConditionList = new ArrayList<String>();
        List<Long> amountList = new ArrayList<Long>();
        Map<String, Long> childMap = new HashMap<String, Long>();
        Map<String, Object> endMap = new HashMap<String, Object>();

        Map<String, Object> sortTotalMap = new HashMap<String, Object>();
        Map<String, Long> sortSecondMap = new HashMap<String, Long>();

        for (FilterConditionVO filterConditionVO : list) {
            if (type.equals(filterConditionVO.getCondition())) {
                if (filterConditionVO.getChildren() != null && filterConditionVO.getChildren().size() != 0) {
                    List<SecondConditionVO> children = filterConditionVO.getChildren();
                    for (SecondConditionVO child : children) {
                        if (child.getSechildren() != null && child.getSechildren().size() != 0) {
                            code = 3;// 三级
                            Map<String, Long> sortMap = new HashMap<String, Long>();
                            Map<String, Long> thirdMap = new HashMap<String, Long>();
                            long totalAmount = 0l;
                            if (endMap.containsKey(child.getCondition())) {
                                sortMap = (Map<String, Long>) sortTotalMap.get(child.getCondition());
                                totalAmount = childMap.get(child.getCondition());
                            }
                            for (ChildConditionVO vo : child.getSechildren()) {
                                totalAmount += vo.getAmount();
                                long count = vo.getAmount();
                                if (sortMap.containsKey(vo.getCondition())) {
                                    count += sortMap.get(vo.getCondition());
                                }
                                sortMap.put(vo.getCondition(), count);
                            }

                            // 最终子集排序取前十
                            List<BarDataVo> barDataVoList=   sortMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(10).
                                    map(v->new BarDataVo(v.getKey(),v.getValue())).collect(Collectors.toList());
                            endMap.put(child.getCondition(), barDataVoList);
                            childMap.put(child.getCondition(), totalAmount);
                            sortTotalMap.put(child.getCondition(), sortMap);
                        } else {
                            code = 2;// 两级
                            if (sortSecondMap.containsKey(child.getCondition())) {
                                int index = seConditionList.indexOf(child.getCondition());
                                long amount = amountList.get(index);
                                amountList.set(index,amount + child.getAmount());
                                sortSecondMap.put(child.getCondition(),sortSecondMap.get(child.getCondition()) + child.getAmount());
                            } else {
                                seConditionList.add(child.getCondition());
                                amountList.add(child.getAmount());
                                sortSecondMap.put(child.getCondition(),child.getAmount());
                            }
                        }
                    }
                } else {
                    code = 1;// 一级
                }
            }
        }
        if (code == 3) {
            resultMap.put("childMap", childMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                    .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new)));
            resultMap.put("endMap", endMap);
        } else if (code == 2) {
            // 最终子集排序取前十
            List<BarDataVo> barList = sortSecondMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(10)
                    .map(v->new BarDataVo(v.getKey(),v.getValue())).collect(Collectors.toList());
            resultMap.put("sechildData", barList);
        }
        resultMap.put("code", code);
        log.info("SearchStatisticsController childrenAjaxData end");
        return resultMap;
    }

    /**
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     * @Title: pageSkipAnalysis
     * @Description: 翻页分析
     */
    @RequestMapping(value = "pageSkipAnalysis",method = RequestMethod.GET)
    public String pageSkipAnalysis() {
        return "/searchStatistic/pageSkipAnalysis";
    }

    @RequestMapping(value = "pageSkipAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> pageSkipAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController pageSkipAjaxData start");
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        List<FlowSearchPaging> pagingList = searchStatisticsClient.getPagingByCategory(queryParam).getData();

        List<BarDataVo> barDataList = new ArrayList<BarDataVo>();
        for (FlowSearchPaging searchPaging : pagingList) {
            String category = CommonSearchType.getShownameByValue(searchPaging.getCategoryCode());
            if (StringUtils.isNotEmpty(category)) {
                BarDataVo vo = new BarDataVo();
                vo.setAmount(searchPaging.getAmount());
                vo.setCategory(category);
                barDataList.add(vo);
            }
        }
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setBarData(barDataList);
        log.info("SearchStatisticsController pageSkipAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    @RequestMapping(value = "pageCodeAjaxData",method = RequestMethod.POST)
    @ResponseBody
    public Result<SearchStatisticsResult> pageCodeAjaxData(SearchStatisticsQuery queryParam) {
        log.info("SearchStatisticsController pageCodeAjaxData start");
        List<String> statisticCodeList = getPlatformCodeList(queryParam.getPlatformCode());
        queryParam.setStatisticCodeList(statisticCodeList);
        queryParam.setCategoryId(CommonSearchType.getValueByShowName(queryParam.getCategoryName()));

        List<FlowSearchPaging> pagingList = searchStatisticsClient.getPagingByPageCode(queryParam).getData();
        int pageNum = 0;
        List<BarDataVo> pageDataList = new ArrayList<>();
        if(pagingList !=null && !pagingList.isEmpty()){
            pageDataList = pagingList.stream().limit(10).map(v->new BarDataVo("页码"+v.getPageNum(),v.getAmount()))
                .collect(Collectors.toList());
            pageNum =pagingList.size();
        }
        SearchStatisticsResult searchStatisticsResult = new SearchStatisticsResult();
        searchStatisticsResult.setPageData(pageDataList);
        searchStatisticsResult.setPageNum(pageNum);
        log.info("SearchStatisticsController pageCodeAjaxData end");
        return Result.success(searchStatisticsResult);
    }

    /**
     * 获取平台code码
     * @param platformCode
     * @return
     */
    private List<String> getPlatformCodeList(String platformCode) {
        List<String> statisticCodeList = new ArrayList<String>();
        // app
        if ("app".equals(platformCode)) {
            statisticCodeList = searchStatisticsClient.getDeviceCodeApp();
        }
        // wap
        else if ("wap".equals(platformCode)) {
            statisticCodeList = searchStatisticsClient.getDeviceCodeByLikeName("wap");
        }
        // pc
        else if ("pc".equals(platformCode)) {
            statisticCodeList = searchStatisticsClient.getDeviceCodeByLikeName("pc");
        } else {
            statisticCodeList = null;
        }
        return statisticCodeList;
    }

    /**
     * 获取渠道名称
     * @param channelCode
     * @return
     */
    private static String getChannelName(String channelCode) {
        if (channelCode.startsWith("12h")) {// SEM code码以12h开头
            return "SEM";
        } else if (channelCode.startsWith("123")) {// SEO code码以123开头
            return "SEO";
        } else if (channelCode.equals("116yq5")) {// 自有渠道 同义 直接流量
            return "直接流量";
        } else {
            return "其他";
        }
    }

    /**
     * 设置折线图数据
     *
     * @param mapForLine
     * @return
     */
    private List<LineDataVo> parseJsonForLine(Map<String, Long> mapForLine) {
        List<LineDataVo> lineList = new ArrayList<>();
        for (Entry<String, Long> entry : mapForLine.entrySet()) {
            LineDataVo lineDataVo = new LineDataVo();
            lineDataVo.setTime(entry.getKey().split("_")[1]);
            lineDataVo.setCategory(entry.getKey().split("_")[0]);
            lineDataVo.setAmount(entry.getValue());
            lineList.add(lineDataVo);
        }
        return lineList;
    }

    /**
     * 设置柱状图数据
     * @param mapForBar
     * @return
     */
    private List<BarDataVo> parseJsonForBar(Map<String, Long> mapForBar) {
        List<BarDataVo> barList = new ArrayList<>();
        for (Entry<String, Long> entry : mapForBar.entrySet()) {
            BarDataVo barDataVo = new BarDataVo();
            barDataVo.setCategory(entry.getKey());
            barDataVo.setAmount(entry.getValue());
            barList.add(barDataVo);
        }
        return barList;
    }

    public static SearchConditionVO getSectionCondition(int sectionId) {
        SearchConditionVO param = new SearchConditionVO();
        switch (sectionId) {
            case 1:
                param.setMaxValue(50);
                param.setMinValue(0);
                return param;
            case 2:
                param.setMinValue(50);
                param.setMaxValue(500);
                return param;
            case 3:
                param.setMinValue(500);
                param.setMaxValue(1000);
                return param;
            case 4:
                param.setMinValue(1000);
                param.setMaxValue(0);
                return param;
            default:
                return param;
        }
    }


    /**
     * @param @param  list
     * @param @param  page_size
     * @param @param  current_page
     * @param @return 设定文件
     * @return PageInfo<T> 返回类型
     * @throws
     * @Title: getListByPage
     * @Description: list分页显示
     */
    private PageInfo<ProductInfoVO> getListByPage(List<ProductInfoVO> list, int pageSize, int currentPage) {
        if (null == list) {
            return null;
        }
        PageInfo<ProductInfoVO> page = new PageInfo<>();
        int totalNum = list.size();
        int pages = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
        int pageEndRow = 0;
        int pageStartRow = 0;
        if (currentPage * pageSize < totalNum) {// 判断是否为最后一页 
            pageEndRow = currentPage * pageSize;
        } else {
            pageEndRow = totalNum;
        }
        pageStartRow = pageSize * (currentPage - 1) + 1;
        page.setStartRow(pageStartRow);
        page.setEndRow(pageEndRow);
        page.setPageSize(pageSize);
        page.setIsFirstPage(currentPage == 1);
        page.setIsLastPage(currentPage == pages);
        page.setHasNextPage(currentPage < pages);
        page.setHasPreviousPage(currentPage > 1);
        page.setPages(pages);
        page.setPageNum(currentPage);
        page.setTotal(totalNum);

        List<ProductInfoVO> resultList = null;
        if (list.size() > 0) {
            resultList = list.subList(pageStartRow - 1, pageEndRow);
        }
        page.setList(resultList);
        return page;
    }

    /**
     * @Title: getVoList
     * @Description: json串转成对象
     * @param @param filterStr
     * @param @param category_code
     * @param @return 设定文件
     * @return List<FilterConditionVO> 返回类型
     * @throws
     */
    private List<FilterConditionVO> getVoList(String filterStr,String categoryCode) {
        List<FilterConditionVO> listVO = new ArrayList<FilterConditionVO>();
        if (StringUtils.isNotBlank(filterStr)) {
            filterStr = filterStr.replaceAll("\r|\n", "");
            JSONObject jsonObject = JSONConversionUtil.stringToObj(filterStr,JSONObject.class);
            for(Entry<String,Object> entry:jsonObject.entrySet()){
                FilterConditionVO vo = new FilterConditionVO();
                String key = entry.getKey();
                String value = entry.getValue().toString();
                if(key.split("&").length==0){
                    log.info("SearchStatisticsController getVoList badData:{}",jsonObject);
                    continue;
                }
                String code = key.split("&")[0];// 传给接口的参数

                String condition1 = "";// 筛选的key值
                String codeValue = ""; // 筛选条件的value值的code
                String condition2 = "";// 筛选条件的value值的中文
                // App 796以后的版本
                if (code.contains("=")) {
                    condition1 = code.split("=")[0];
                    codeValue = code.split("=")[1];
                    if (key.split("&").length>1){
                        condition2 = key.split("&")[1];
                    }else {
                        log.info("SearchStatisticsController getVoList badData:{}",jsonObject);
                        continue;
                    }
                    if (condition1.equals("hotelStar")) {
                        condition2 = condition2.replaceAll("级", "");
                    }
                    FlowSearchFilterCondition param = new FlowSearchFilterCondition();
                    param.setCategoryCode(categoryCode);
                    param.setCode(codeValue);
                    param.setKey(condition1);
                    if (searchStatisticsClient.getFilterNameByCode(param) == null) {
                        param.setName(condition2);
                       searchStatisticsClient.insertFilter(param);
                    }
                } else {
                    // APP 794,795版本的filter数据;h5的数据都走这边
                    condition1 = code;
                    if(key.split("&").length>1){
                        codeValue = key.split("&")[1];
                    }
                    if ("price".equals(code) || "sort".equals(code) || "hotelStar".equals(code)) {
                        if (code.equals("sort")) {
                            condition2 = CommonStringType.getSortNameByValue(codeValue);
                            if (condition2.equals("0") || codeValue.equals("0")) {
                                condition2 = "其他";
                            }
                            // 796之后版本sort传中文
                            if (StringUtils.isEmpty(condition2) && (!"今日可订".equals(codeValue))) {
                                condition2 = codeValue;
                            }
                        } else if (code.equals("price")) {
                            // 各平台794,795数据格式不一致，特殊处理
                            if ("HOTEL".equals(categoryCode)) {
                                condition2 = CommonStringType.getHotelPriceNameByValue(codeValue);
                                if (StringUtils.isEmpty(condition2)) {
                                    if (codeValue.indexOf("150") > -1) {
                                        condition2 += "￥150以下,";
                                    }
                                    if (codeValue.indexOf("300") > -1) {
                                        condition2 += "￥151-￥300,";
                                    }
                                    if (codeValue.indexOf("450") > -1) {
                                        condition2 += "￥301-￥450,";
                                    }
                                    if (codeValue.indexOf("600") > -1) {
                                        condition2 = "￥451-￥600,";
                                    }
                                    if (codeValue.indexOf("1000") > -1) {
                                        condition2 += "￥601-￥1000";
                                    }
                                    if (codeValue.indexOf("1001") > -1) {
                                        condition2 += "￥1001以上";
                                    }
                                }
                                if (condition2.endsWith(",")) {
                                    condition2 = condition2.substring(0,condition2.length() - 1);
                                }
                            } else {
                                condition2 = CommonStringType.getRoutePriceNameByValue(codeValue);
                            }
                        } else if (code.equals("hotelStar")) {
                            condition2 = CommonStringType.getHotelStarNameByValue(codeValue).replaceAll("级", "");
                            if (StringUtils.isEmpty(condition2)) {
                                if (codeValue.indexOf("100") > -1) {
                                    condition2 += "五星/豪华型,";
                                }
                                if (codeValue.indexOf("102") > -1) {
                                    condition2 += "四星/品质型,";
                                }
                                if (codeValue.indexOf("104") > -1) {
                                    condition2 += "三星/舒适型,";
                                }
                                if (codeValue.indexOf("106") > -1) {
                                    condition2 = "二星/简约型,";
                                }
                                if (codeValue.indexOf("109") > -1) {
                                    condition2 += "其他/其他";
                                }
                            }
                            if (condition2.endsWith(",")) {
                                condition2 = condition2.substring(0,condition2.length() - 1);
                            }
                        }
                    } else {
                        // 之前版本 多选情况
                        if ("routeNum".equals(condition1)) {
                            String strArr[] = codeValue.split(",");
                            for (String str : strArr) {
                                FlowSearchFilterCondition param = new FlowSearchFilterCondition();
                                param.setCategoryCode(categoryCode);
                                param.setCode(str);
                                param.setKey(code);
                                FlowSearchFilterCondition filterCondition = searchStatisticsClient.getFilterNameByCode(param).getData();
                                if (filterCondition != null) {
                                    condition2 += filterCondition.getName() + ",";
                                } else {
                                    condition2 = codeValue + ",";
                                }
                            }
                            condition2 = condition2.substring(0, condition2.length() - 1);
                        } else {
                            FlowSearchFilterCondition param = new FlowSearchFilterCondition();
                            param.setCategoryCode(categoryCode);
                            param.setCode(codeValue);
                            param.setKey(code);
                            FlowSearchFilterCondition filterCondition = searchStatisticsClient.getFilterNameByCode(param).getData();
                            if (filterCondition != null) {
                                condition2 = filterCondition.getName();
                            } else {
                                condition2 = codeValue;
                            }
                        }

                    }
                }
                String conditionStr = CommonSearchFilter.getShowName(condition1, categoryCode);
                // 判断此筛选条件是否有父级，有父级则会有三层统计数据，没有父级则有两级，但是今日可订特殊只需一级统计
                if (StringUtils.isNotEmpty(conditionStr) && StringUtils.isNotEmpty(condition2)) {
                    if (CommonSearchFilter.hasParentFlag(condition1,categoryCode)) {
                        List<SecondConditionVO> children = new ArrayList<SecondConditionVO>();
                        String parentConditionStr = CommonSearchFilter.getParentShowName(condition1, categoryCode);
                        // 二级对象
                        SecondConditionVO secondVO = new SecondConditionVO();
                        secondVO.setCondition(conditionStr);
                        secondVO.setAmount(Long.parseLong(value));
                        if (!(conditionStr.indexOf("景点活动") > -1 || conditionStr.indexOf("促销活动") > -1)) {
                            List<ChildConditionVO> thirdChildren = new ArrayList<ChildConditionVO>();
                            // 三级 若出现多选情况 ，用逗号分开
                            String[] conditionArr = condition2.split(",");
                            for (String condition : conditionArr) {
                                ChildConditionVO childVO = new ChildConditionVO();
                                childVO.setCondition(condition);
                                childVO.setAmount(Long.parseLong(value));
                                thirdChildren.add(childVO);
                            }
                            secondVO.setSechildren(thirdChildren);
                        }
                        children.add(secondVO);
                        vo.setCondition(parentConditionStr);
                        vo.setAmount(Integer.parseInt(value));
                        vo.setChildren(children);
                    } else {
                        if (!"todayTicketFlag".equals(condition1)) {
                            List<SecondConditionVO> children = new ArrayList<SecondConditionVO>();
                            SecondConditionVO secondVO = new SecondConditionVO();
                            secondVO.setCondition(condition2);
                            secondVO.setAmount(Long.parseLong(value));
                            children.add(secondVO);
                            vo.setChildren(children);
                        }
                        vo.setCondition(conditionStr);
                        vo.setAmount(Integer.parseInt(value));
                    }

                    listVO.add(vo);
                }
            }
        }
        return listVO;
    }

}